<?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/12/26
     * Time: 15:11
     */

    class Baby_model extends CI_Model
    {
        public $limit_rows = 15;

        /**
         * 根据分类名获取cateId
         *
         * @param $cate_name
         *
         * @return mixed
         * author: walker
         * Date: 2019/11/6
         * Time: 15:30
         * Note:
         */
        public function baby_goods_num($_param = [],$type = 0)
        {
            if (!empty($_param['baby_goods_name'])){
                $_param['baby_goods_name'] =  trim($_param['baby_goods_name']);
                $this->db->where("(baby_goods_name like '%{$_param['baby_goods_name']}%' OR baby_goods_sn = '{$_param['baby_goods_name']}')");
            }
            $this->db->where(['baby_type'=>$type]);
            return $this->db->from('baby_goods')->count_all_results();
            
        }

        public function baby_goods_list($_param = [],$type = 0){
            if (!empty($_param['baby_goods_name'])){
                $_param['baby_goods_name'] =  trim($_param['baby_goods_name']);
                $this->db->where("(baby_goods_name like '%{$_param['baby_goods_name']}%' OR baby_goods_sn = '{$_param['baby_goods_name']}')");
            }
            if ($_param['admin_id'] > 0){
                $this->db->where(['baby_admin_id'=>$_param['admin_id']]);
            }

            $this->db->where(['baby_type'=>$type]);
            $baby_goods = $this->db->from('baby_goods')->order_by('id desc')->limit($this->limit_rows,$this->limit_rows * $_param['page'])->get()->result_array();
            return $baby_goods;
        }

        public function goods_sn_max($admin_id,$type = 0){
            $baby_goods = $this->db->from('baby_goods')->where(['baby_admin_id'=>$admin_id])->select('baby_goods_sn')->order_by('id desc')->get()->row_array();
            $admin = $this->db->from('admin')->where(['admin_id'=>$admin_id])->select('ab_name')->get()->row_array();
            if($type == 0){
                if($baby_goods){
                    $goods_sn_max = 'WW' . $admin['ab_name'] . (substr($baby_goods['baby_goods_sn'],4)+1);
                }else{
                    $goods_sn_max = 'WW' . $admin['ab_name'] . 1000;
                }
            }else{
                if($baby_goods){
                    $goods_sn_max = 'CW' . $admin['ab_name'] . (substr($baby_goods['baby_goods_sn'],4)+1);
                }else{
                    $goods_sn_max = 'CW' . $admin['ab_name'] .  1000;
                }
            }
            
            return $goods_sn_max;
        }

        public function baby_img($id){
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$id,'sort'=>1])->select('baby_goods_img')->get()->row_array();
            if(empty($baby_goods_img)){
                $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$id])->select('baby_goods_img')->get()->row_array();
            }
            return $baby_goods_img['baby_goods_img'];
        }

        public function baby_salegoods($_param,$prepare_state){
            $siteweb = $this->db->from('siteweb')->where(['type'=>3,'is_delete'=>0])->where_in('web_id',explode(',',substr($prepare_state,1)))->select('web_id,web_name')->get()->result_array();
            if($_param['goods_name']){
                $this->db->where("(baby_goods_sn like '%{$_param['goods_name']}%')");
            }
            
            $baby_goods = $this->db->from('baby_goods')->limit($this->limit_rows,$this->limit_rows * $_param['page'])->select('id,baby_goods_sn')->get()->result_array();
            foreach ($baby_goods as $key => $value) {
                $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$value['id']])->select('baby_goods_img')->get()->row_array();
                $baby_goods[$key]['baby_goods_img'] = $baby_goods_img['baby_goods_img'];
                $web = [];
                foreach ($siteweb as $k => $v) {
                    if ($_param['order_time_start'] && $_param['order_time_end']) {
                        $this->db->where("oi.add_time >= '{$_param['order_time_start']}' and oi.add_time <= '{$_param['order_time_end']}'");
                    }
                    // echo $v['web_id'];die;
                    $this->db->where(['og.goods_id'=>$value['id']]);
                    $this->db->where(['oi.web_id'=>$v['web_id']]);
                    $result = $this->db->from('order_goods as og')
                        ->join('order_info as oi', 'og.order_id = oi.order_id', 'left')->select('count(*) as count')
                        ->get()->row_array();
                    $web[$v['web_id']] = $result['count'];
                }
                $baby_goods[$key]['sum'] = array_sum($web);
                $baby_goods[$key]['web'] = $web;
            }
            return $baby_goods;
            // if($_param['siteweb']){
            //     $this->db->where_in(['oi.web_id'=>$_param['siteweb']]);
            // }else{
            //     $siteweb = $this->db->from('siteweb')->where(['type'=>3,'is_delete'=>0])->select('web_id,web_name')->get()->result_array();
            //     $web = [];
            //     foreach ($siteweb as $key => $value) {
            //         $web[] = $value['web_id'];
            //     }
            //     $this->db->where_in('oi.web_id',$web);
            // }
            // if ($_param['order_time_start'] && $_param['order_time_end']) {
            //     $this->db->where("oi.add_time >= '{$_param['order_time_start']}' and oi.add_time <= '{$_param['order_time_end']}'");
            // }

            // $result = $this->db->from('order_goods as og')
            //     ->join('order_info as oi', 'og.order_id = oi.order_id', 'left')
            //     ->get()->result_array();
            // return $result;
        }

        public function statistics_num($_param = []){
            if (!empty($_param['goods_name'])){
                $_param['baby_goods_sn'] =  trim($_param['goods_name']);
                // $this->db->where("(baby_goods_name like '%{$_param['baby_goods_name']}%' OR baby_goods_sn = '{$_param['baby_goods_name']}')");
                $this->db->where(['baby_goods_sn'=>$_param['baby_goods_sn']]);
            }
            $this->db->where(['baby_type'=>1]);
            return $this->db->from('baby_goods')->count_all_results();
        }
    }